package net.esj.alipay.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.esj.alipay.config.AlipayConfig;
import net.esj.alipay.util.AlipaySubmit;
import net.esj.basic.utils.StringUtils;
import net.melon.order.inst.service.OrderService;
import net.melon.order.pojo.Order;
import net.melon.order.pojo.Order.OrderState;
import net.melon.product.inst.service.impl.ClassifyCacheProxy;
import net.melon.product.pojo.ProductClassify;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class AlipayServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7121938306145611765L;
	
	
	private OrderService orderService;
	
	private ClassifyCacheProxy classifyCacheProxy;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		////////////////////////////////////请求参数//////////////////////////////////////
		response.setContentType("text/html;charset=UTF-8");
		
		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		ServletContext servletContext = this.getServletContext();
		
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		orderService = (OrderService) context.getBean("orderService");
		
		classifyCacheProxy = (ClassifyCacheProxy)context.getBean("classifyCacheProxy");
		
		//商户订单号，商户网站订单系统中唯一订单号，必填
		String out_trade_no = request.getParameter("out_trade_no");
		
		if(StringUtils.hasText(out_trade_no)){
			Order order = orderService.getOrderById(out_trade_no);
			if(order!=null && order.getState()==OrderState.NO_PAY.getState()){
				DecimalFormat df = new DecimalFormat("######0.00"); 
				ProductClassify classify = classifyCacheProxy.getClassify(order.getProduct().getClassifyId());
				order.getProduct().setClassify(classify);
				String subject = classify.getName() + " " +order.getProduct().getName();
				//String total_fee = df.format(order.getPrepay());
				String total_fee = df.format(0.01);
				//把请求参数打包成数组
				Map<String, String> sParaTemp = new HashMap<String, String>();
				
				sParaTemp.put("service", AlipayConfig.service);
				sParaTemp.put("partner", AlipayConfig.partner);
				sParaTemp.put("seller_id", AlipayConfig.seller_id);
				sParaTemp.put("_input_charset", AlipayConfig.input_charset);
				sParaTemp.put("payment_type", AlipayConfig.payment_type);
				sParaTemp.put("notify_url", AlipayConfig.notify_url);
				sParaTemp.put("return_url", AlipayConfig.return_url);
				sParaTemp.put("anti_phishing_key", AlipayConfig.anti_phishing_key);
				sParaTemp.put("exter_invoke_ip", AlipayConfig.exter_invoke_ip);
				sParaTemp.put("out_trade_no", out_trade_no);
				sParaTemp.put("subject", subject);
				sParaTemp.put("total_fee", total_fee);
				sParaTemp.put("body", "车壹订单");
				//建立请求
				String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,"get","确认");
				out.println(sHtmlText);
			}
			
		}
		
	}
	
}
